package com.mindata.blockchain;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

/**
 * 发布者  用于触发事件 关于时间的相关内容参考 https://www.jianshu.com/p/e03c5c53d2e9
 * @author wuweifeng on 2018/3/13.
 */
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

//    装配springboot容器
    @Override
    public void setApplicationContext(ApplicationContext ac)
            throws BeansException {
        context = ac;
    }

//    容器通过类的类 获取 类
    public static <T> T getBean(Class<T> tClass) {
        return context.getBean(tClass);
    }

//      容器通过类的名称 和 类的类 获取类
    public static <T> T getBean(String name, Class<T> tClass) {
        return context.getBean(name, tClass);
    }

//容器触发事件
    public static void publishEvent(ApplicationEvent event) {
        context.publishEvent(event);
    }
}
